701C - They Are Everywhere - CodeForces Solution


binary search strings two pointers *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define el '\n'
#define dd long double
#define ff float
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define sc second
#define re reserve
#define re1 v.resize
#define re2 a.resize
#define in insert
#define lp(i, a, b) for(int i = a; i <= b; i++)
#define lp1(i, a, b) for(int i = a; i < b; i++)
#define ln(i,k,n)    for (int i=n ; i>=k ; i--)
#define ln1(i,k,n)    for (int i=n ; i>k ; i--)
using namespace std;
//continue
// @author: saleh_zizo
//std::reverse(b.begin(), b.end());
const ll mod = 1e9;
const ll N = 1e6+1;
bool prime[N];
//ll d[N];
//======================================================
void input()
{
#ifndef ONLINE_JUDGE
    freopen("islands.in","r",stdin);
    freopen("output.txt","w",stdout);
#endif
}
//======================================================
void fast() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
//======================================================
void sieve(){ // O(n * (log(log(n)))
    memset(prime, true, sizeof prime);
    prime[0] = prime[1] = false;
    for(int i = 2; i * i < N; i++){
        if(prime[i]){
            for(int j = i * i; j < N; j += i){
                prime[j] = false;
            }
        }
    }
}
//======================================================
pair<ll, ll>pi(ll h , ll m , ll cnt) {
	while (cnt--) {
		m++;
		if (m == 60) {h++; m = 0;}
		if (h == 24) {h = 0;}
	}
	return {h, m};
}
//======================================================
vector<int> divisors(int n){ // O(sqrt(n))
    vector<int>v;
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0){
            v.push_back(i);
            if(n / i != i)
                v.push_back(n / i);
        }
    }
    return v;
}
//======================================================
vector<ll>primeFactorization(ll n){// O(sqrt(n))
    vector<ll>v;
    for(ll i=2;i*i<=n;i++){
       // vector<ll>temp;
        while(n%i==0){
            v.pb(i);
            n/=i;
        }
    }
    if(n>1){
        v.pb(n);
    }
    return v;
}
//======================================================
ll checked_true(ll mid){
    ll c = (mid*(mid+1))/2;
    return c;
}
//======================================================
ll F(ll n){
    if(n==0){
        return 0;
    }
    if(n==1){
        return 1;
    }
    return F(n-1) + F(n-2);
}
//======================================================
int main() {
fast();
// 0 1 2 [0 , 1 , 2 , '\0'];
//input()
//cout<<fixed<<setprecision(10);// 1 2 3 4 5
    //sieve();
    //    5
    // 1 2 3 0 3 8
    ll n; cin>>n;
    string x; cin>>x;
    map<char , ll> mp , temp;
    lp1(i,0,n){
        mp[x[i]]++;
    }
    ll l = 0 , r = 0 , ans = mod;
    while(l < n){
        while(temp.size() < mp.size() && r < n){
            temp[x[r++]]++;
        }
        if(temp.size() == mp.size()){
            //cout<<l<<' '<<r<<el;
            ans = min(ans , r - l);
        }
        temp[x[l]]--;
        if(temp[x[l]] == 0){
            temp.erase(x[l]);
        }
        l++;
    }
    cout<<ans<<el;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST